package com.weixin.project.Service;

import com.weixin.project.Dao.NoticeDao;
import com.weixin.project.Entity.ForumQuestion;
import com.weixin.project.Entity.Notice;
import org.aspectj.weaver.ast.Not;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class NoticeService {
    @Value("10")
    Integer pageSize;
    @Autowired
    RedisTemplate redisTemplate;
    @Autowired
    NoticeDao noticeDao;

    private final static String NOTICE = "notice";
    private final static String NOTICE_LIST = "notice_list";

    @CachePut(value = NOTICE, key = "#result.id", unless = "#result == null ")
    public Notice storeNotice(Notice notice){
        return noticeDao.save(notice);
    }

    @Cacheable(value = NOTICE, key = "#noticeId", unless = "#result == null ")
    public Notice findNotice(Integer noticeId){
        Optional<Notice> noticeOp = noticeDao.findById(noticeId);
        return noticeOp.isPresent()?noticeOp.get():null;
    }

    public Page<Notice> findNotices(Integer page){
        Sort sort = Sort.by("id").ascending();
        Pageable pageable = PageRequest.of(page, pageSize, sort);
        Page<Notice> noticePage = noticeDao.findAll(pageable);
        //不管是否存在key:forum_question_total_pages都执行
        return noticePage;
    }

}
